package com.zz.ecommerce.goodsManage.goodsBackupInsp.service;

import com.zz.ecommerce.common.CommonDao;
import com.zz.ecommerce.common.bean.TSendToSupervisionTask;
import com.zz.ecommerce.common.bean.insp.db.goodsbackup.TGoodsBackupAttachInsp;
import com.zz.ecommerce.common.bean.insp.db.goodsbackup.TGoodsBackupHeadInsp;
import com.zz.ecommerce.fileupload.UploadFile;
import com.zz.ecommerce.fileupload.UploadFileDao;
import com.zz.ecommerce.fileupload.UploadFileService;
import com.zz.ecommerce.goodsManage.goodsBackupInsp.dao.GoodsBackupAttachInspDao;
import com.zz.ecommerce.goodsManage.goodsBackupInsp.dao.GoodsBackupHeadInspDao;
import com.zz.ecommerce.utils.DateUtil;
import com.zz.ecommerce.utils.SessionUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2015-12-23.
 */
@Service
public class GoodsBackupInspService {
    @Autowired
    private GoodsBackupHeadInspDao headInspDao;

    @Autowired
    private GoodsBackupAttachInspDao attachInspDao;

    @Autowired
    private CommonDao commonDao;

    @Autowired
    private UploadFileDao uploadFileDao;

    @Autowired
    private UploadFileService uploadFileService;

    public String getEportSeq()
    {
        return commonDao.getEportSeq();
    }

    @Transactional
    public void saveHead(TGoodsBackupHeadInsp goodsBasic, HttpSession session) {
        if (StringUtils.isEmpty(goodsBasic.getId())) {
            goodsBasic.setId(UUID.randomUUID().toString());
            goodsBasic.setStatus("E0");
            goodsBasic.setStatusTime(DateUtil.nowTime());
            goodsBasic.setOperId(SessionUtil.getOperator(session));
            goodsBasic.setOrgId(SessionUtil.getOrganizationId(session));
            headInspDao.insert("T_GOODS_BACKUP_HEAD_INSP", goodsBasic);
        } else {
            headInspDao.update("T_GOODS_BACKUP_HEAD_INSP", goodsBasic,
                    " WHERE ID =#{updateObj.id}");
        }
    }

    public void saveAttach(TGoodsBackupAttachInsp attach, HttpSession session) {
        if (StringUtils.isEmpty(attach.getId())) {
            attach.setId(UUID.randomUUID().toString());
            attach.setOperId(SessionUtil.getOperator(session));
            attach.setOrgId(SessionUtil.getOrganizationId(session));
//            attach.setAnnexcontent(base64File(attach.getFileId()));
            attachInspDao.insert("T_GOODS_BACKUP_ATTACH_INSP", attach);
        } else {
            attachInspDao.update("T_GOODS_BACKUP_ATTACH_INSP", attach,
                    " WHERE id =#{updateObj.id}") ;
        }
    }

    @Transactional
    public void deleteHeadInsp(TGoodsBackupHeadInsp goodsBasic) {
        String appIdZ = goodsBasic.getId();
        String[] appids = appIdZ.split(",");
        if (StringUtils.isNotEmpty(appIdZ) && appids.length > 0) {
            String deleteCondition = "";
            for (String appid : appids) {
                deleteCondition += StringUtils.isNotEmpty(appid) ? "'" + appid + "'," : "";
            }

            deleteCondition = deleteCondition.substring(0, deleteCondition.lastIndexOf(","));
            if (StringUtils.isNotEmpty(deleteCondition)) {
                headInspDao.delete("T_GOODS_BACKUP_HEAD_INSP",
                        " WHERE ID IN(" + deleteCondition + ")") ;

                attachInspDao.delete("T_GOODS_BACKUP_ATTACH_INSP",
                        " WHERE RELATE_ID IN(" + deleteCondition + ")");
            }
        }
    }

    @Transactional
    public void deleteGoodsAttach(TGoodsBackupAttachInsp attach) {
        TGoodsBackupAttachInsp basic = new TGoodsBackupAttachInsp();
        basic.setId(attach.getId());
        String appIdZ = attach.getId();
        String[] appids = appIdZ.split(",");
        if (StringUtils.isNotEmpty(appIdZ) && appids.length > 0) {
            String deleteCondition = "";
            for (String appid : appids) {
                deleteCondition += StringUtils.isNotEmpty(appid) ? "'" + appid + "'," : "";
            }

            deleteCondition = deleteCondition.substring(0, deleteCondition.lastIndexOf(","));
            if (StringUtils.isNotEmpty(deleteCondition)) {
                 attachInspDao.delete("T_GOODS_BACKUP_ATTACH_INSP",
                        " WHERE ID IN(" + deleteCondition + ")");
            }
        }
    }
    @Transactional
    public void declare(List<String> idList) throws Exception {
        for (String id : idList) {
            TGoodsBackupHeadInsp basic = new TGoodsBackupHeadInsp();
            basic.setId(id);
            basic = headInspDao.query("T_GOODS_BACKUP_HEAD_INSP", basic);
            if (null == basic) {
                continue;
            }

            insertSendTask("T_GOODS_BACKUP_HEAD_INSP", "goodsserialno", basic.getGoodsserialno());

            updateBusiStatus(basic);

        }
    }

    private void insertSendTask(String tableName, String idName, String idValue) {
        TSendToSupervisionTask taskBean = new TSendToSupervisionTask();
        taskBean.setTaskId(UUID.randomUUID().toString());
        taskBean.setTableName(tableName);
        taskBean.setIdName(idName);
        taskBean.setIdValue(idValue);
        taskBean.setIsSend("0");
        taskBean.setType("SDEPORT");
        commonDao.insert("ECOMMERCE.T_SEND_TO_SUPERVISION_TASK", taskBean);
    }

    private void updateBusiStatus(TGoodsBackupHeadInsp basic) {
        TGoodsBackupHeadInsp update = new TGoodsBackupHeadInsp();
        update.setId(basic.getId());

        String nowTime = DateUtil.nowTime();
//        update.setApplytime(nowTime);
        update.setStatusTime(nowTime);
        update.setStatus("E1");

        headInspDao.update("T_GOODS_BACKUP_HEAD_INSP",
                update, " WHERE ID=#{updateObj.id} ");
    }

    private byte[] byteFile(String fileId) {
        if (StringUtils.isEmpty(fileId)) {
            return null;
        }

        List<UploadFile> uploadFileList = uploadFileDao.queryFilesById(fileId);
        if (CollectionUtils.isEmpty(uploadFileList)) {
            return null;
        }

        byte[] fileContent = uploadFileList.get(0).getFileContent();
        return fileContent;
    }

    private String base64File(String fileId) {
        if (StringUtils.isEmpty(fileId)) {
            return "";
        }

        List<UploadFile> uploadFileList = uploadFileDao.queryFilesById(fileId);
        if (CollectionUtils.isEmpty(uploadFileList)) {
            return "";
        }

        byte[] fileContent = uploadFileList.get(0).getFileContent();
        if (fileContent != null) {
            return Base64.encodeBase64String(fileContent);
        }

        return "";
    }
}
